package com.wondersgroup.insurance.dnuilearn.anim;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;

import com.wondersgroup.insurance.dnuilearn.R;

/**
 * Created by Administrator on 2017/6/12.
 */

public class TBDetailActvity extends AppCompatActivity {
    ImageView image1, image2;


    int duration = 300;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tb_detail_one);
        image1 = (ImageView) findViewById(R.id.image1);
        image2 = (ImageView) findViewById(R.id.image2);
        image1.setOnClickListener(clickListener);
        image2.setOnClickListener(clickListener);


    }

    View.OnClickListener clickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.image1:
                    showTypeAnim();
                    break;
                case R.id.image2:
                    hideTypeAnim();
                    break;
            }
        }
    };

    void showTypeAnim(){

        AnimatorSet set = new AnimatorSet();
        PropertyValuesHolder holder1 = PropertyValuesHolder.ofFloat("alpha", 1f,0.5f);
        PropertyValuesHolder holder2 = PropertyValuesHolder.ofFloat("rotationX", 0,20f);
//        PropertyValuesHolder holder3 = PropertyValuesHolder.ofFloat("scaleX", 1f,0.7f);

        ObjectAnimator   objectAnimator = ObjectAnimator.ofPropertyValuesHolder(image1, holder1,holder2);

        ObjectAnimator objectAnimator2 = ObjectAnimator.ofFloat(image2, "translationY",0.0f,(float)(-image2.getHeight()));

        objectAnimator.start();


        set.play(objectAnimator).with(objectAnimator2);
        set.setDuration(duration);
        set.start();

        ObjectAnimator objectAnimator3 = ObjectAnimator.ofFloat(image1, "scaleX",1.0f,0.8f);
        ObjectAnimator objectAnimator4 = ObjectAnimator.ofFloat(image1, "rotationX",20f,0f);
        AnimatorSet set1 = new AnimatorSet();
        set1.play(objectAnimator3).with(objectAnimator4);
        set1.setStartDelay(duration);
        set1.setDuration(100);
        set1.start();

    }

    void hideTypeAnim(){


        ObjectAnimator objectAnimator3 = ObjectAnimator.ofFloat(image1, "scaleX",0.8f,1.0f);
        ObjectAnimator objectAnimator4 = ObjectAnimator.ofFloat(image1, "rotationX",0f,20f);

        AnimatorSet set1 = new AnimatorSet();
        set1.play(objectAnimator3).with(objectAnimator4);
        set1.setDuration(100);



        AnimatorSet set = new AnimatorSet();

        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(image2, "translationY",(float)(-image2.getHeight()),0);


        PropertyValuesHolder holder1 = PropertyValuesHolder.ofFloat("alpha", 0.5f,1f);
        PropertyValuesHolder holder2 = PropertyValuesHolder.ofFloat("rotationX", 20f,0);
//        PropertyValuesHolder holder3 = PropertyValuesHolder.ofFloat("scaleX", 1f,0.7f);

        ObjectAnimator   objectAnimator2 = ObjectAnimator.ofPropertyValuesHolder(image1, holder1,holder2);
        set.play(objectAnimator).with(objectAnimator2);
        set.setDuration(duration);


        set1.start();
        set.setStartDelay(100);
        set.start();
    }


  /*  public void startFirstAnim(View v) {
        //显示first_view:1.透明；2.缩放；3.翻转动画（以什么地方为原点进行翻转？）；4.
        //缩放
        ObjectAnimator firstScaleXAnim = ObjectAnimator.ofFloat(first_view, "scaleX", 1.0f,0.8f);
        firstScaleXAnim.setDuration(300);
        ObjectAnimator firstScaleYAnim = ObjectAnimator.ofFloat(first_view, "scaleY", 1.0f,0.8f);
        firstScaleYAnim.setDuration(300);
        //透明度
        ObjectAnimator firstAlphaAnim = ObjectAnimator.ofFloat(first_view, "alpha", 1.0f,0.5f);
        firstAlphaAnim.setDuration(300);
        //旋转
        ObjectAnimator firstRotationXAnim = ObjectAnimator.ofFloat(first_view, "rotationX", 0f,20f);
        firstRotationXAnim.setDuration(200);
        //再旋转回来
        ObjectAnimator firstResumeRotationXAnim = ObjectAnimator.ofFloat(first_view, "rotationX",20f, 0f);
        firstResumeRotationXAnim.setDuration(200);
        firstResumeRotationXAnim.setStartDelay(200);//延迟第一次旋转动画的时间，在这之后执行
        //由于缩放造成离顶部有个距离，需要平移上去
        ObjectAnimator firstTranlationYAnim = ObjectAnimator.ofFloat(first_view, "translationY", 0f,-0.1f*first_view.getHeight());
        firstTranlationYAnim.setDuration(300);

        //第二个view和第一个view动画同时开始执行
        ObjectAnimator secondTranlationYAnim = ObjectAnimator.ofFloat(second_view, "translationY", second_view.getHeight(),0f);
        secondTranlationYAnim.setDuration(300);
        secondTranlationYAnim.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationStart(Animator animation) {`
                super.onAnimationStart(animation);
                second_view.setVisibility(View.VISIBLE);
                bt.setClickable(false);
            }

        });
        AnimatorSet set = new AnimatorSet();
        set.playTogether(firstScaleXAnim,firstScaleYAnim,firstAlphaAnim,firstRotationXAnim,firstResumeRotationXAnim,firstTranlationYAnim,secondTranlationYAnim);
        set.start();
    }

    public void startSecondAnim(View v) {
        //显示first_view:1.透明；2.缩放；3.翻转动画（以什么地方为原点进行翻转？）；4.
        //缩放
        ObjectAnimator firstScaleXAnim = ObjectAnimator.ofFloat(first_view, "scaleX",0.8f, 1.0f);
        firstScaleXAnim.setDuration(300);
        ObjectAnimator firstScaleYAnim = ObjectAnimator.ofFloat(first_view, "scaleY",0.8f, 1.0f);
        firstScaleYAnim.setDuration(300);
        //透明度
        ObjectAnimator firstAlphaAnim = ObjectAnimator.ofFloat(first_view, "alpha",0.5f, 1.0f);
        firstAlphaAnim.setDuration(300);
        //旋转
        ObjectAnimator firstRotationXAnim = ObjectAnimator.ofFloat(first_view, "rotationX", 0f,20f);
        firstRotationXAnim.setDuration(200);
        //再旋转回来
        ObjectAnimator firstResumeRotationXAnim = ObjectAnimator.ofFloat(first_view, "rotationX",20f, 0f);
        firstResumeRotationXAnim.setDuration(200);
        firstResumeRotationXAnim.setStartDelay(200);//延迟第一次旋转动画的时间，在这之后执行
        //由于缩放造成离顶部有个距离，需要平移上去
        ObjectAnimator firstTranlationYAnim = ObjectAnimator.ofFloat(first_view, "translationY",-0.1f*first_view.getHeight(), 0f);
        firstTranlationYAnim.setDuration(300);

        //第二个view和第一个view动画同时开始执行
        ObjectAnimator secondTranlationYAnim = ObjectAnimator.ofFloat(second_view, "translationY",0f, second_view.getHeight());
        secondTranlationYAnim.setDuration(300);
        secondTranlationYAnim.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                // TODO Auto-generated method stub
                super.onAnimationEnd(animation);
                second_view.setVisibility(View.INVISIBLE);
                bt.setClickable(true);
            }
        });
        AnimatorSet set = new AnimatorSet();
        set.playTogether(firstScaleXAnim,firstScaleYAnim,firstAlphaAnim,firstRotationXAnim,firstResumeRotationXAnim,firstTranlationYAnim,secondTranlationYAnim);
        set.start();
    }*/
}
